// license-header java merge-point
/**
 * This is only generated once! It will never be overwritten.
 * You can (and have to!) safely modify it by hand.
 */
package fr.upstlse.glre.business.domain.user;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import fr.upstlse.glre.business.vo.user.UserSearchCriteriaVO;

/**
 * @see fr.upstlse.glre.business.domain.user.User
 */
public class UserDaoImpl extends
		fr.upstlse.glre.business.domain.user.UserDaoBase {
	/**
	 * @see fr.upstlse.glre.business.domain.UserDao#toUserVO(fr.upstlse.glre.business.domain.User, fr.upstlse.glre.business.vo.UserVO)
	 */
	public void toUserVO(fr.upstlse.glre.business.domain.user.User source,
			fr.upstlse.glre.business.vo.user.UserVO target) {
		super.toUserVO(source, target);
	}

	/**
	 * @see fr.upstlse.glre.business.domain.UserDao#toUserVO(fr.upstlse.glre.business.domain.User)
	 */
	public fr.upstlse.glre.business.vo.user.UserVO toUserVO(
			final fr.upstlse.glre.business.domain.user.User entity) {
		return super.toUserVO(entity);
	}

	/**
	 * Retrieves the entity object that is associated with the specified value object
	 * from the object store. If no such entity object exists in the object store,
	 * a new, blank entity is created
	 */
	private fr.upstlse.glre.business.domain.user.User loadUserFromUserVO(
			fr.upstlse.glre.business.vo.user.UserVO userVO) {
		fr.upstlse.glre.business.domain.user.User user = this.load(userVO
				.getId());
		if (user == null) {
			user = fr.upstlse.glre.business.domain.user.User.Factory
					.newInstance();
		}
		return user;
	}

	/**
	 * @see fr.upstlse.glre.business.domain.UserDao#userVOToEntity(fr.upstlse.glre.business.vo.UserVO)
	 */
	public fr.upstlse.glre.business.domain.user.User userVOToEntity(
			fr.upstlse.glre.business.vo.user.UserVO userVO) {
		fr.upstlse.glre.business.domain.user.User entity = this
				.loadUserFromUserVO(userVO);
		this.userVOToEntity(userVO, entity, true);
		return entity;
	}

	/**
	 * @see fr.upstlse.glre.business.domain.UserDao#userVOToEntity(fr.upstlse.glre.business.vo.UserVO, fr.upstlse.glre.business.domain.User)
	 */
	public void userVOToEntity(fr.upstlse.glre.business.vo.user.UserVO source,
			fr.upstlse.glre.business.domain.user.User target, boolean copyIfNull) {
		super.userVOToEntity(source, target, copyIfNull);
	}

	@Override
	protected List handleFindByCriteria(
			UserSearchCriteriaVO pUserSearchCriteriaVO) throws Exception {
		// Create User criteria
		Criteria lUserCriteria = getSession().createCriteria(User.class);

		if (!StringUtils.isEmpty(pUserSearchCriteriaVO.getLogin())){
			if(StringUtils.contains(pUserSearchCriteriaVO.getLogin(), "%")) {
				lUserCriteria.add(Restrictions.like("login", pUserSearchCriteriaVO
						.getLogin()));
			}
			else {
				lUserCriteria.add(Restrictions.eq("login", pUserSearchCriteriaVO
						.getLogin()));
			}
		}

		if (!StringUtils.isEmpty(pUserSearchCriteriaVO.getPassword())){
			if(StringUtils.contains(pUserSearchCriteriaVO.getPassword(), "%")) {
				lUserCriteria.add(Restrictions.like("password", pUserSearchCriteriaVO
						.getPassword()));
			}
			else {
				lUserCriteria.add(Restrictions.eq("password", pUserSearchCriteriaVO
						.getPassword()));
			}
		}

		if (pUserSearchCriteriaVO.getRole() != null){
			if(StringUtils.contains(pUserSearchCriteriaVO.getRole().getValue(), "%")) {
				lUserCriteria.add(Restrictions.like("role", pUserSearchCriteriaVO
						.getRole()));
			}
			else {
				lUserCriteria.add(Restrictions.eq("role", pUserSearchCriteriaVO
						.getRole()));
			}
		}

		return lUserCriteria.list();
	}

	@Override
	public Object create(int pTransform, User pUser) {
		if (pUser == null)
        {
            throw new IllegalArgumentException(
                "User.create - 'user' can not be null");
        }
        this.getHibernateTemplate().save(pUser);
        return this.transformEntity(pTransform, pUser);
	}
	

}